<%@ page import="cap.dao.ChatRoomDao" %>
<%@ page import="cap.dao.impl.ChatRoomDaoImpl" %>
<%@ page import="cap.bean.ChatRoomVo" %>
<%@ page import="java.util.List" %>
<%@ page import="cap.bean.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    User user=(User)request.getSession().getAttribute("user");
    String username=user.getUserName();
    request.getSession().setAttribute("speaking",username);
    request.getSession().setAttribute("nickname",username);
    ChatRoomDao chatRoomDao=new ChatRoomDaoImpl();
%>
<html>
<head>
    <title>聊天室广场</title>
    <link rel="stylesheet" type="text/css" href="css/footer.css">

</head>
<div id="path" style="display: none"><%=basePath%></div>
<body background="picture/logind.jpg" style="background-size: cover;background-attachment: fixed;background-color: white">
<c:if test="${null!=user}">
<div style="width: 400px;height: 100px;">
    <table>
        <tr>
            <td>用户名</td>
            <td><%=username%></td>
            <td><input type="button" value="退出系统" style="border-radius: 3px;width: 70px" onclick="logout()"></td>
        </tr>
        <tr>
            <td>用户昵称</td>
            <td><%=username%></td>
            <td><input type="button" value="修改昵称" style="border-radius: 3px;width: 70px" onclick="work()"></td>
        </tr>
        <tr>
            <td>聊天室数目</td>
            <td><%=chatRoomDao.getchatroomnumbers()%></td>
            <td><input type="button" value="刷新" style="border-radius: 3px;width: 40px" onclick="refresh()">
                <input type="button" value="创建聊天室" style="border-radius: 3px;width: 120px" onclick="create()"></td>
        </tr>
    </table>
</div>
<hr style="border:  2px solid black">
<div id="info">
    <table>
        <tr>
            <td>聊天室名称</td>
            <td>聊天室创建者</td>
            <td>人数上限</td>
            <td>聊天室内当前人数</td>
            <td>聊天室简介</td>
            <td></td>
        </tr>
        <%
            List<ChatRoomVo> they = chatRoomDao.getChatRoomVo();
            for(int i=0;i<they.size();i++){
        %>
        <tr>
            <td><%=they.get(i).getRoomname()%></td>
            <td><%=they.get(i).getRoomhost()%></td>
            <td><%=they.get(i).getRoomnumber()%></td>
            <td><%=they.get(i).getRoomonline()%></td>
            <td><%=they.get(i).getContent()%></td>
            <td><button onclick="attend()" id=<%=1+i%>>加入</button></td>
        </tr>
        <%
            }
        %>
    </table>
</div>
<div id="footer" >
</div>
</c:if>
<c:if test="${null==user}">
    <jsp:include page="404.jsp"/>
</c:if>
</body>
<script>
    function create() {
        var xmlhttp = false;
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }
        xmlhttp.open("GET","createRoom.jsp",true);
        xmlhttp.onreadystatechange = function (ev) {
            if(xmlhttp.readyState==4){
                info.innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.send();
    }
    function refresh() {
        window.location.href = "chatRoom.jsp";
    }
    function work() {
        alert("暂未完成");
    }
    function attend() {
        var btn = document.getElementsByTagName("button");
        var id;
        document.body.onclick = function (ev) {
            id = ev.target.id;
            sessionStorage.setItem("room",id);
            document.cookie = "room="+id;
        }
        <%
        Cookie cookie = null;
        Cookie[] cookies = null;
        cookies = request.getCookies();
        String room = "";
        if(cookies!=null){
            for(int i=0;i<cookies.length;i++){
                cookie = cookies[i];
                if(cookie.getName().equals("room")){
                    room = cookie.getValue();
                }
            }
        }
        session.setAttribute("room",room);
    %>
        var path=document.getElementById("path").innerText;
        window.location.href = path+"/attend";
    }
    function logout() {
        var path=document.getElementById("path").innerText;
        window.location.href=path+"user?method=index";
    }
</script>
</html>
